package com.ruoyi.classai.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.domain.PxTrainingCourseComplete;
import com.ruoyi.classai.domain.PxTrainingMessage;
import com.ruoyi.classai.domain.PxTrainingTeacher;
import com.ruoyi.classai.mapper.PxTrainingMessageMapper;
import com.ruoyi.classai.service.IPxTrainingCourseCompleteService;
import com.ruoyi.classai.service.IPxTrainingCourseService;
import com.ruoyi.classai.service.IPxTrainingMessageService;
import com.ruoyi.classai.service.IPxTrainingTeacherService;
import com.ruoyi.common.core.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * 培训消息Service业务层处理
 * 
 * @author lcj
 * @date 2025-06-25
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PxTrainingMessageServiceImpl extends ServiceImpl<PxTrainingMessageMapper, PxTrainingMessage> implements IPxTrainingMessageService {

    @Lazy
    @Autowired
    private IPxTrainingCourseService trainingCourseService;
    @Autowired
    private IPxTrainingTeacherService trainingTeacherService;
    @Autowired
    private IPxTrainingCourseCompleteService trainingCourseCompleteService;

    @Override
    public void addPxMeaage() {
        //时间转换
        DateTime dateTime = DateUtil.offsetDay(DateUtil.date(), -1);
        String time=DateUtil.format(dateTime,"yyyy-MM-dd");
        //查询课程
        List<PxTrainingCourse> list=trainingCourseService.list(Wrappers.<PxTrainingCourse>lambdaQuery()
                .eq(PxTrainingCourse::getStatus, Constants.COMMON_SUCCESS)
                .apply("date_format(start_time,'%Y-%m-%d') ="+time));
        //循环课程
        list.forEach(d->{
            //查询课程下的学生
            List<PxTrainingTeacher> teachers=trainingTeacherService.list(Wrappers.<PxTrainingTeacher>lambdaQuery().eq(PxTrainingTeacher::getTrainingId,d.getTrainingId()));
            for (PxTrainingTeacher teacher : teachers) {
                //查询老师是否完成
                PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                        .eq(PxTrainingCourseComplete::getTeacherId,teacher.getTeacherId())
                        .eq(PxTrainingCourseComplete::getTrainingCourseId,d.getId())
                        .eq(PxTrainingCourseComplete::getStatus,Constants.COMMON_SUCCESS));
                if (Objects.isNull(courseComplete)){
                    //设置提醒
                    PxTrainingMessage message=new PxTrainingMessage();
                    message.setMsgType(d.getTrainingFormat()); //培训形式
                    message.setMsgTitle("作业提醒");
                    message.setMsgContent("您的("+d.getModuleTitle()+")课程作业,暂未完成,请及时完成作业");
                }
            }
        });

        /*最后一天提醒*/
        List<PxTrainingCourse> list1=trainingCourseService.list(Wrappers.<PxTrainingCourse>lambdaQuery()
                .eq(PxTrainingCourse::getStatus, Constants.COMMON_SUCCESS)
                .apply("date_format(work_end_time,'%Y-%m-%d') ="+DateUtil.format(DateUtil.date(),"yyyy-MM-dd")));
        list1.forEach(d->{
            //查询是否有课程结束时间
            //查询课程下的学生
            List<PxTrainingTeacher> teachers=trainingTeacherService.list(Wrappers.<PxTrainingTeacher>lambdaQuery().eq(PxTrainingTeacher::getTrainingId,d.getTrainingId()));
            for (PxTrainingTeacher teacher : teachers) {
                //查询老师是否完成
                PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                        .eq(PxTrainingCourseComplete::getTeacherId,teacher.getTeacherId())
                        .eq(PxTrainingCourseComplete::getTrainingCourseId,d.getId())
                        .eq(PxTrainingCourseComplete::getStatus,Constants.COMMON_SUCCESS));
                if (Objects.isNull(courseComplete)){
                    //设置提醒
                    PxTrainingMessage message=new PxTrainingMessage();
                    message.setMsgType(d.getTrainingFormat()); //培训形式
                    message.setMsgTitle("作业提醒");
                    message.setMsgContent("您的("+d.getModuleTitle()+")课程作业,暂未完成,请及时完成作业");
                }
            }
        });
    }

}
